/** 
 * @Description: 仿真雷达
 * @Author: 程前
 * @Date: 2025-02-17 08:58:54
 * @LastEditors: 程前
 * @LastEditTime: 2025-02-20 14:59:45
 */

import { Viewer, Entity, Color, ScreenSpaceEventType, ScreenSpaceEventHandler, Cartesian3, Math, CallbackProperty, JulianDate, Clock } from "cesium";
import { setEventByHandler, removeEventByHandler } from "@/utils";

export default class SimRadar {
    private viewer: Viewer;
    private radarEntity: Entity | null;
    private scanEntity: Entity | null;
    private handler: ScreenSpaceEventHandler;
    private config: any;

    constructor(viewer: Viewer, handler: ScreenSpaceEventHandler) {
        this.viewer = viewer;
        this.handler = handler;
        this.radarEntity = null;
        this.scanEntity = null;
        this.config = {
            radius: 1000,          // 雷达半径
            height: 1000,           // 雷达高度
            speed: 1,            // 扫描速度
            baseColor: Color.TRANSPARENT,   // 基础颜色
            scanColor: Color.GREY.withAlpha(0.6),   // 扫描扇形颜色
            scanAngle: 60.0,       // 扫描扇形角度
            rotationSpeed: 6.0     // 旋转速度（秒/圈）
        };

        // 确保时钟在运行
        this.viewer.clock.shouldAnimate = true;

        // 初始化事件
        this.initEvents();
    }

    /**
     * 初始化事件
     */
    initEvents() {
        const leftClick = (event: any) => {
            if (!event.position) return;
            const ray = this.viewer.camera.getPickRay(event.position);
            if (!ray) return;
            const position = this.viewer.scene.globe.pick(ray, this.viewer.scene);
            if (!position) return;
            this.createRadarEntities(position);
        }
        setEventByHandler(this.handler, leftClick, ScreenSpaceEventType.LEFT_CLICK);
    }

    /**
     * 创建雷达实体
     * @param position 位置
     */
    private createRadarEntities(position: Cartesian3) {
        // 移除现有实体
        if (this.radarEntity) {
            this.viewer.entities.remove(this.radarEntity);
        }
        if (this.scanEntity) {
            this.viewer.entities.remove(this.scanEntity);
        }

        // 创建雷达主体
        this.radarEntity = this.viewer.entities.add({
            name: "雷达主体",
            position: position,
            ellipsoid: {
                radii: new Cartesian3(this.config.radius, this.config.radius, this.config.height),
                innerRadii: new Cartesian3(1, 1, 1),
                minimumCone: Math.toRadians(40.0),
                maximumCone: Math.PI_OVER_TWO,
                material: this.config.baseColor,
                outline: true,
                outlineColor: Color.BLUE.withAlpha(0.5),
                outlineWidth: 3,
                slicePartitions: 32,
                stackPartitions: 32
            }
        });

        // 创建扫描扇形
        this.scanEntity = this.viewer.entities.add({
            name: "扫描扇形",
            position: position,
            ellipsoid: {
                radii: new Cartesian3(this.config.radius, this.config.radius, this.config.height),
                innerRadii: new Cartesian3(1.0, 1.0, 1.0),
                minimumCone: Math.toRadians(40.0),
                maximumCone: Math.toRadians(80.0),
                minimumClock: new CallbackProperty((time: JulianDate) => {
                    const angle = -(JulianDate.totalDays(time) * 24 * 60 * 60 * Math.TWO_PI / this.config.rotationSpeed) % Math.TWO_PI;
                    return angle;
                }, false),
                maximumClock: new CallbackProperty((time: JulianDate) => {
                    const baseAngle = -(JulianDate.totalDays(time) * 24 * 60 * 60 * Math.TWO_PI / this.config.rotationSpeed) % Math.TWO_PI;
                    return baseAngle + Math.toRadians(this.config.scanAngle);
                }, false),
                material: this.config.scanColor,
                outline: false,
                slicePartitions: 64,
                stackPartitions: 64
            }
        });
    }
    
    /**
     * 更新扫描速度
     * @param speed 速度（秒/圈）
     */
    updateRotationSpeed(speed: number) {
        this.config.rotationSpeed = speed;
    }

    /**
     * 销毁
     */
    destroy() {
        if (this.handler) {
            removeEventByHandler(this.handler, ScreenSpaceEventType.LEFT_CLICK);
            this.handler.destroy();
        }
        if (this.radarEntity) {
            this.viewer.entities.remove(this.radarEntity);
        }
        if (this.scanEntity) {
            this.viewer.entities.remove(this.scanEntity);
        }
    }
}